package com.mikao.service.impl;

import com.mikao.dao.TicketDao;
import com.mikao.dto.bo.ReqDataTableDto;
import com.mikao.dto.bo.TicketForm;
import com.mikao.dto.vo.RspDataTableDto;
import com.mikao.entity.Ticket;
import com.mikao.service.TicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import java.util.List;

/**
 * @author kenny
 */

@Service
public class TicketServiceImpl implements TicketService {

    private TicketDao ticketDao;

    public TicketServiceImpl(TicketDao ticketDao) {
        this.ticketDao = ticketDao;
    }

    @Override
    public RspDataTableDto<Ticket> ticketList(ReqDataTableDto reqDataTableDTO) {
        List<Ticket> all = ticketDao.findAll();
        return new RspDataTableDto<>(reqDataTableDTO.getDraw(), (long) all.size(), all);
    }

    @Override
    public void deleteTicket(Long id) {
        ticketDao.delete(id);
    }

    @Override
    public void saveTicket(TicketForm ticketForm) {
        Ticket ticket;
        if (ObjectUtils.isEmpty(ticketForm.getId())) {
            ticket = new Ticket();
        } else {
            ticket = ticketDao.findOne(ticketForm.getId());
        }
        BeanUtils.copyProperties(ticketForm, ticket);
        ticketDao.saveAndFlush(ticket);
    }


}
